package oct2013;

public class Candy {

	public int candy(int[] ratings) {
		int n = ratings.length;
		int sum = 0;
		int[] num = new int[n];
		for (int i = 0; i < n; ++i) {
			int cnt = 2;
			num[i] = 1;
			while (i + 1 < n && ratings[i + 1] > ratings[i]) {
				num[i + 1] = cnt++;
				i++;
			}
		}
		for (int i = n - 1; i >= 0; --i) {
			int cnt = 2;
			while (i - 1 >= 0 && ratings[i - 1] > ratings[i]) {
				num[i - 1] = Math.max(num[i - 1], cnt++);
				i--;
			}
		}
		for (int i = 0; i < n; ++i)
			sum += num[i];
		return sum;
	}
}
